var api = {};

api.getRootUrl = function() {
	return 'https://shop.coopbuy.com';
//	return 'http://192.168.10.25:8006';
};

var apiurl = {
    MemberBankCardBindApply:''
};

api.getFileBaseUrl = function() {
	return 'https://oss.coopbuy.com';
//	return 'http://192.168.10.248:10555';
};

api.getFileUrl = function(path) {
	return api.getFileBaseUrl() + path;
}

api.getRequestUrl = function(url) {
	return api.getRootUrl() + "/" + url;
};

api.getApiServiceUrl = function(url) {
	return api.getRootUrl() + "/api/services/" + url;
};

api.ajax = function(url, option) {
	option.headers = {

	};
	mui.ajax(url, option);
};

api.post = function(url, option) {
	option.type = 'post';
	option.dataType = 'json';
	option.timeout = 60000;
	if(option.headers === undefined || option.headers === null) {
		option.headers = {};
	}
	option.headers['Content-Type'] = 'application/json';
	if(gnAuth.isLogin()) {
		var token = gnAuth.getUserInfo();
		if(token != null) {
			option.headers['Authorization'] = token.tokenType + " " + token.token;
		}
	}
	if(typeof(option.data) === 'object') {
		option.data = JSON.stringify(option.data);
	}
	mui.ajax(url, option);

}

function errorHandle(res) {
	if(res != null) {
		if(res.error.code === -1) {
			gnAuth.clearUserinfo();
			gnAuth.openLogin();
		} else {
			gnApp.alert('出错', res.error.message, '确定');

		}
	} else {
		gnApp.toast("网络发生异常");
	}
}

api.postController = function(url, data, callback) {

	mui.ajax(url, {    
		data: data,
		type: 'POST',
		contentType: "application/json; charset=utf-8",
		success: function(data) {
			callback && callback(data);
		},
		error: function(msg)  {   
			errorHandle(msg);    
		}    
	});    
}

api.postApiService = function(url, data, callback, errorCallback) {

	api.post(api.getApiServiceUrl(url), {
		data: data,

		success: function(data) {
			if(data.success) {

				callback && callback(data.result);
			} else {

				if(data.error.code === -1) {
					gnAuth.clearUserinfo();
					gnApp.toast(toastR.m9001);
				} else {
					gnApp.alert('出错', data.error.message, '确定');
				}
			}
		},
		error: function(msg, type) {
			if(type == 'timeout') {
				gnApp.toast(toastR.m9002);
			} else {
				try {
					var res = JSON.parse(msg.response);
					errorHandle(res);
					errorCallback && errorCallback(res);
				} catch(e) {
					gnApp.toast(toastR.m9003);
				}
			}
		}
	});
}

//业务
api.addProToCart = function(skuIds, callback) {
	gnApp.showWaiting();
	api.postApiService(apiurl.cart.addGoods, {
		goods: skuIds
	}, function() {
		gnApp.closeWaiting();
		gnApp.toast(toastR.m2005);
		gnApp.refreshUser();
		callback && callback();
	}, function() {
		gnApp.closeWaiting();
	});
};

var gnAuth = {};
gnAuth.isStationUser = function() {
	if(typeof(localStorage.$gnUser) == 'undefined') {
		return false;
	}
	var userinfo = JSON.parse(localStorage.$gnUser);
	if(userinfo.isMaster) {
		return true;
	} else {
		return false;
	}
};

gnAuth.clearUserinfo = function() {
	localStorage.removeItem('$gnUser');
};

gnAuth.getUserInfo = function() {
	var userStr = localStorage.$gnUser;
	if(userStr) {
		return JSON.parse(userStr);
	} else {
		return null;
	}

};

gnAuth.setUserInfo = function($gnuser) {
	localStorage.$gnUser = JSON.stringify($gnuser);
};

gnAuth.isLogin = function() {
	return typeof(localStorage.$gnUser) != 'undefined' && localStorage.$gnUser != null;
};

gnAuth.loginCallback = null;

gnAuth.openLogin = function(callback, callbackPageId, callbackPageEvent) {
	gnApp.showWaiting();
	var hasView = plus.webview.getWebviewById('login');
	if(hasView) {
		hasView.close('none');
	}

	var loginView = plus.webview.create('/pages/user/login.html', 'login', {}, {
		pageId: callbackPageId,
		pageEvent: callbackPageEvent
	});

	var currentView = plus.webview.currentWebview();
	if(gnAuth.loginCallback != null) {
		currentView.removeEventListener("loginSuccess", gnAuth.loginCallback);
	}
	gnAuth.loginCallback = function() {
		callback && callback();
	};
	currentView.addEventListener("loginSuccess", gnAuth.loginCallback, false);
	//	loginView.addEventListener("loaded", function() {
	//		gnApp.closeWaiting();
	//		loginView.show('slide-in-right', 200);
	//	}, false);
};

gnAuth.checkLogin = function(hasLoginCallback, callbackPageId, callbackPageEvent) {
	if(!gnAuth.isLogin()) {
		gnAuth.openLogin(callbackPageId, callbackPageEvent);
	} else {
		hasLoginCallback && hasLoginCallback();
	}
};